package com.tmy.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmy.Dto.CommonDto;
import com.tmy.Dto.PageInfo;
import com.tmy.Dto.Result;
import com.tmy.entity.Hotel;
import com.tmy.entity.RoomInfo;
import com.tmy.mapper.HotelMapper;
import com.tmy.mapper.RoomInfoMapper;
import com.tmy.service.RoomInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoomInfoServiceImpl implements RoomInfoService {

    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private HotelMapper hotelMapper;

    /**
     * 新增房型
     */
    @Override
     public Result addRoom(RoomInfo roomInfo){
         Result result =new Result();
         try {
             roomInfoMapper.insert(roomInfo);
             result.setCode(200);
             result.setMsg("房型添加成功");
         }catch (Exception err){
             result.setCode(500);
             result.setMsg(err.getCause());
         }
         return result;
     }
    /**
     * 删除房型
     */
    @Override
    public Result deleteRoom(RoomInfo roomInfo){
        Result result =new Result();
        try {
            roomInfoMapper.deleteById(roomInfo);
            result.setCode(200);
            result.setMsg("房型删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 修改房型
     */
    @Override
    public Result updateRoom(RoomInfo roomInfo){
        Result result =new Result();
        try {
            roomInfoMapper.updateById(roomInfo);
            result.setCode(200);
            result.setMsg("房型修改成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询房型（分页查询）
     */
    @Override
    public Result queryRoomPage(PageInfo pageInfo){
        Result result =new Result();
        try {
            Page<RoomInfo> page = new Page<>(pageInfo.getPage(),pageInfo.getLimit());
            RoomInfo roomInfo = JSON.parseObject(JSONObject.toJSONString(pageInfo.getParam()),RoomInfo.class);
            if (roomInfo.getGrade()!=null){

            }
            Page<RoomInfo> roomInfoPage = roomInfoMapper.selectPage(page, null);
            for (RoomInfo item:roomInfoPage.getRecords()){
                Hotel hotel = hotelMapper.selectById(item.getHotelId());
                item.setHotelInfo(JSONObject.from(hotel));
            }
            result.setData(roomInfoPage);
            result.setCode(200);
            result.setMsg("房型查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 批量删除接口
     */
    @Override
    public Result deleteRooms(CommonDto commonDto){
        Result result =new Result();
        try {
           List<Integer> ids = commonDto.getDeleteIds();
            roomInfoMapper.deleteBatchIds(ids);
            result.setCode(200);
            result.setMsg("房型删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
}
